<template>
  <div class="dialog-box">
    <el-dialog width="600px" :title="dialogTitle" v-model="dialogFormVisible" :draggable="true" @close="dialogClose">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules">
        <el-row>
          <el-col :span="12">
            <el-form-item prop="typeId" label="类型">
              <el-select style="width:90%" v-model="dialogForm.typeId" filterable clearable @change="typeChange">
                <el-option v-for="item in typeOptions" :key="item.typeId" :label="item.typeName" :value="item.typeId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="categoryId" label="分类">
              <el-select style="width:90%" v-model="dialogForm.categoryId" filterable clearable no-data-text="请先选择类型"
                @change="cateChange">
                <el-option v-for="item in simpleOptions" :key="item.categoryId" :label="item.categoryName"
                  :value="item.categoryId" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item prop="brandId" label="品牌">
              <el-select style="width:90%" v-model="dialogForm.brandId" filterable clearable no-data-text="请先选择分类">
                <el-option v-for="item in brandOptions" :key="item.brandId" :label="item.brandName" :value="item.brandId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="channelProductName" label="上游商品名称">
              <el-input style="width:90%" v-model="dialogForm.channelProductName" clearable placeholder="请输入上游商品名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider border-style="dashed" />
        <el-form-item prop="channelProductInfo" label="商品信息">
          <el-input type="textarea" v-model="dialogForm.channelProductInfo" clearable placeholder="请输入商品信息" />
        </el-form-item>
        <!-- <el-divider border-style="dashed" />
        <el-row>
          <el-col :span="12">
            <el-form-item prop="password" label="登录密码">
              <el-input style="width:90%" v-model="dialogForm.password" type="password" show-password placeholder="请输入登录密码" />
            </el-form-item>
          </el-col>
        </el-row> -->
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitHandle">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from 'vue'
import hexMD5 from "@/utils/md5.min.js"
import api from '../../api'
import { successDeal, warningDeal } from '@/utils/utils'
export default {
  setup(props, context) {
    const dialogFormRef = ref(null)

    const state = reactive({
      dialogTitle: '',
      dialogFormVisible: false,
      dialogForm: {},
      dialogFormRules: {
        typeId: [
          { required: true, message: '请选择类型', trigger: 'blur' },
        ],
        categoryId: [
          { required: true, message: '请选择分类', trigger: 'blur' },
        ],
        brandId: [
          { required: true, message: '请选择品牌', trigger: 'blur' },
        ],
        channelProductName: [
          { required: true, message: '请输入上游商品名称', trigger: 'blur' },
        ],
        channelProductInfo: [
          { required: true, message: '请输入商品信息', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
        ],
      },

      typeOptions: [],
      simpleOptions: [],
      brandOptions: [],
    })

    function dialogShow(type, data) {
      state.dialogForm = data
      if (type == 'add') {
        state.dialogTitle = '新增上游商品'
      } else {
        state.dialogTitle = '编辑上游商品'
        getSimOptions(data.typeId)
        goodsBrandSimple(data.categoryId)
      }
      state.dialogFormVisible = true
      getTypeOptions()
    }

    function submitHandle() {
      dialogFormRef.value.validate((valid, fields) => {
        if (valid) {
          if (!state.dialogForm.recordId) {
            addHandle()
          } else {
            editHandle()
          }
        }
      })
    }

    function addHandle() {
      const obj = {
        channelCode: state.dialogForm.channelCode || null,
        typeId: state.dialogForm.typeId || null,
        categoryId: state.dialogForm.categoryId || null,
        brandId: state.dialogForm.brandId || null,
        channelProductInfo: state.dialogForm.channelProductInfo || null,
        channelProductName: state.dialogForm.channelProductName || null,
        // password: hexMD5(state.dialogForm.password),
      }
      api.productAdd(obj).then(res => {
        if (res.code == '200') {
          successDeal(res.msg)
          context.emit('dialogClose')
          state.dialogFormVisible = false
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function editHandle() {
      const obj = {
        recordId: state.dialogForm.recordId || null,
        channelCode: state.dialogForm.channelCode || null,
        typeId: state.dialogForm.typeId || null,
        categoryId: state.dialogForm.categoryId || null,
        brandId: state.dialogForm.brandId || null,
        channelProductInfo: state.dialogForm.channelProductInfo || null,
        channelProductName: state.dialogForm.channelProductName || null,
        // password: hexMD5(state.dialogForm.password),
      }
      api.productModify(obj).then(res => {
        if (res.code == '200') {
          successDeal(res.msg)
          context.emit('dialogClose')
          state.dialogFormVisible = false
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function dialogClose() {
      dialogFormRef.value.resetFields()
    }

    function typeChange(e) {
      state.dialogForm.categoryId = null
      state.dialogForm.brandId = null
      if (!e) {
        state.simpleOptions = []
        state.brandOptions = []
        return
      }
      getSimOptions(e)
    }

    function cateChange(e) {
      state.dialogForm.brandId = null
      if (!e) {
        state.brandOptions = []
        return
      }
      goodsBrandSimple(e)
    }

    function getSimOptions(typeId) {
      const obj = {
        typeId
      }
      api.goodsCategoryListSimple(obj).then(res => {
        if (res.code == '200') {
          state.simpleOptions = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function getTypeOptions() {
      api.goodsTypeListSimple().then(res => {
        if (res.code == '200') {
          state.typeOptions = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function goodsBrandSimple(categoryId) {
      const obj = {
        categoryId
      }
      api.goodsBrandSimple(obj).then(res => {
        if (res.code == '200') {
          state.brandOptions = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    return {
      ...toRefs(state),
      dialogFormRef,
      dialogClose,
      submitHandle,
      dialogShow,
      typeChange,
      cateChange,
    }
  }
}
</script>

<style>
</style>